package org.celery.client.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.celery.client.service.ClientOrderService;
import org.celery.modules.goods.entity.GoodsPackage;
import org.celery.modules.goods.entity.GoodsStyle;
import org.celery.modules.goods.service.IGoodsPackageService;
import org.celery.modules.other.entity.LogisticsFee;
import org.celery.modules.other.service.ILogisticsFeeService;
import org.celery.modules.user.service.IUserDomainService;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@AllArgsConstructor
@Service
public class ClientOrderServiceImpl implements ClientOrderService {

    private final IGoodsPackageService goodsPackageService;
    private final ILogisticsFeeService logisticsFeeService;
    private final IUserDomainService userDomainService;

    @Override
    public BigDecimal computeTotalAmount(String associatedGoodsString) {
        Map<String, Object> associatedGoods = JsonUtil.toMap(associatedGoodsString);

        BigDecimal totalAmount = BigDecimal.valueOf(0);
        for (String styleId : associatedGoods.keySet()) {
            Map<String, Object> item = (Map<String, Object>) associatedGoods.get(styleId);
            Map<String, Object> info = (Map<String, Object>) item.get("info");
            Integer count = (Integer) item.get("count");
            BigDecimal price = new BigDecimal(info.get("price").toString());

            totalAmount = totalAmount.add(new BigDecimal(count).multiply(price));
        }

        return totalAmount;
    }

    @Override
    public BigDecimal computeLogisticsFee(String address, String domain) {
        User user = userDomainService.selectUserByDomain(domain);
        if (Func.isEmpty(user)) {
            return new BigDecimal(0);
        }

        List<LogisticsFee> logisticsFeeList = logisticsFeeService.list(Wrappers.<LogisticsFee>lambdaQuery()
                .eq(LogisticsFee::getTenantId, user.getTenantId())
                .orderByDesc(LogisticsFee::getWeight)
        );
        for (LogisticsFee logisticsFee : logisticsFeeList) {
            List<String> areaStringList = Func.toStrList(",", logisticsFee.getMatchString());
            for (String areaString: areaStringList) {
                if (address.contains(areaString)) {
                    return logisticsFee.getFee();
                }
            }
        }
        return new BigDecimal(0);
    }

    @Override
    public Integer computeCount(String associatedGoodsString) {
        Map<String, Object> associatedGoods = JsonUtil.toMap(associatedGoodsString);

        int count = 0;
        for (String styleId : associatedGoods.keySet()) {
            Map<String, Object> item = (Map<String, Object>) associatedGoods.get(styleId);
            count += (Integer) item.get("count");
        }

        return count;
    }

    @Override
    public Long computeGoodsBusinessId(String associatedGoodsString) {
        try {
            Map<String, Object> associatedGoods = JsonUtil.toMap(associatedGoodsString);

            for (String styleId : associatedGoods.keySet()) {
                Map<String, Object> item = (Map<String, Object>) associatedGoods.get(styleId);
                Map<String, Object> goodsStyle = (Map<String, Object>) item.get("info");
                if (!Func.isNull(goodsStyle) && !Func.isNull(goodsStyle.get("createUser"))) {
                    return Long.parseLong(goodsStyle.get("createUser").toString());
                }
            }
        } catch (Exception e) {
            return 0L;
        }
        return 0L;
    }
}
